<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Email extends CI_Controller {

    public $layout = ADMINLAYOUT;

    function __construct() {
        parent::__construct();
    }

    function _send_email($data) {
        $this->load->library('email');

        $this->email->from(get_ci_config('site_email'), get_ci_config('site_name'));
        $this->email->to($data['email']);
        $this->email->subject($data['subject']);
        $this->email->message($this->load->view('emails/' . $data['user_language'] . '/layout-html', $data, TRUE));
        $this->email->set_alt_message($this->load->view('emails/' . $data['user_language'] . '/' . $data['type'] . '-txt', $data, TRUE));
        $this->email->send();
    }

    function report ($report_id = NULL) {
        $this->load->model('user_panel/user');
        $this->load->model('user_panel/report');

        if($this->input->post('submit') != NULL) {
            $post = $this->input->post();
            unset($post['submit']);

            $report = new Report();
            $report->where('id', $report_id)->get();
            $user = new User();
            $user->where('id', $report->user_id)->get();
            $data['email'] = $user->email;

            switch($report->type) {
                case 'error':
                    $type = 'reply_error';
                    break;
                case 'suggestion':
                    $type = 'reply_suggestion';
                    break;
                default:
                    set_flashdata('Ningún email ha sido enviado porque este tipo de reporte no está asociado a ningún formato de email.');
                    redirect('admin/email/report');
            }

            $this->lang->load('email');
            $data['subject'] = lang('email_title_'.$type, get_ci_config('site_name'));

            $this->load->model('user_panel/user_profile');
            $user_profile = new User_profile();
            $user_profile->where('user_id', $user->id)->get();

            $this->load->model('user_panel/language');
            $language = new Language();
            $language->where('language_id', $user_profile->language)->get();
            $data['user_language'] = $language->language_label;

            $data['type'] = $type;

            $data['site_name'] = get_ci_config('site_name');
            $data['message_data']['report'] = $report->report;
            $this->_send_email($data);

            $report->resolved = TRUE;
            db_save($report);

            set_flashdata('Email enviado correctamente.');
            redirect('admin/email/report');
        }

        $report = new Report();
        $user = new User();

        $data['message'] = flashdata('message');
        if($report_id == NULL) {
            $data['report'] = $report->where('resolved', FALSE)->get();
            $data['user'] = $user;
            $this->load->view('report', $data);

        } else {
            $data['report'] = $report->where('id', $report_id)->get();
            $data['user'] = $user->where('id', $report->user_id)->get();
            $this->load->view('reply_report', $data);
        }

    }
}

/* End of file home.php */
/* Location: ./application/modules/home/controllers/home.php */